package com.aiobc.file.config;

import com.aiobc.file.intercept.DomainInterceptor;
import com.aiobc.file.intercept.ValidPreInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @author wangjun18 2020/3/8
 * @version v1.0.0
 */
@Configuration
public class GlobalCoresConfig {
    /**
     * 拦截处理（请求权限校验）
     * 
     * @return
     */
    @Bean
    ValidPreInterceptor validPreInterceptor() {
        return new ValidPreInterceptor();
    }

    /**
     * 跨域
     * 
     * @return
     */
    @Bean
    DomainInterceptor domainInterceptor() {
        return new DomainInterceptor();
    }

    @Bean
    public CorsFilter corsFilter() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*"); // 1 设置访问源地址
        corsConfiguration.addAllowedHeader("*"); // 2 设置访问源请求头
        corsConfiguration.addAllowedMethod("*"); // 3 设置访问源请求方法
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", corsConfiguration); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }

}
